package web.cms;


import domain.cms.AbstractPage;
import domain.cms.CmsPage;
import org.apache.wicket.Component;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountIndexedParam;
import service.facade.user.CmsUserService;
import web.error.PageNotFound;
import web.panels.TopMenuPanel;

@MountPath(path = "page")
@SuppressWarnings("serial")
@MountIndexedParam
public final class CmsMainPage extends CmsBasePage {


    @SpringBean
    CmsUserService cmsUserService;
    Long sectionId;
    Long categoryId;
    Long pageId;
    IModel<CmsPage> pageModel;

    public CmsMainPage() {
        super();
        setStatelessHint(true);

        //aby login panel nehazel chybu
        pageModel = new LoadableDetachableModel<CmsPage>() {

            @Override
            protected CmsPage load() {
                return new CmsPage();
            }
        };
        getRequestCycle().setRedirect(true);
        setResponsePage(HomePage.class);
    }


    public CmsMainPage(PageParameters params) {
        super(params);
        setStatelessHint(true);

        pageModel = new CmsPageModel(params.getString("0"));
        Integer level = pageModel.getObject().getLevel();
        switch (level) {
            case 0: {
                //  section = true;
                pageId = Long.valueOf(0);
                categoryId = Long.valueOf(0);
                sectionId = pageModel.getObject().getId();
                break;
            }
            case 1: {
                // category = true;
                pageId = Long.valueOf(0);
                categoryId = pageModel.getObject().getId();
                sectionId = pageModel.getObject().getParent().getId();
                break;
            }
            default: {
                //   page = true;
                pageId = pageModel.getObject().getId();
                categoryId = pageModel.getObject().getParent().getId();
                sectionId = pageModel.getObject().getParent().getParent().getId();
            }
        }

        add(new Label("content", new PropertyModel<String>(pageModel, AbstractPage.CONTENT)).setEscapeModelStrings(false));
//        add(new Label("pageTitle", new PropertyModel<String>(pageModel, "title")).setEscapeModelStrings(false));
    }

    @Override
    public IModel getPageTitle() {
        return new PropertyModel(pageModel, AbstractPage.TITLE);
    }

    @Override
    public IModel getDescription() {
        return new PropertyModel(pageModel, AbstractPage.DESCRIPTION);

    }

    @Override
    public IModel getKeywords() {
        return new PropertyModel(pageModel, AbstractPage.KEYWORDS);
    }

    @Override
    protected Component newTopPanel(String id) {
        if (sectionId == null) {
            return new EmptyPanel(id);
        }

        return new TopMenuPanel(id, sectionId);
    }

    @Override
    protected Component newLeftPanel(String id) {
        if (sectionId == null) {
            return new EmptyPanel(id);
        }
        IModel<CmsPage> sectionModel = new LoadableDetachableModel<CmsPage>() {

            @Override
            protected CmsPage load() {
                return cmsUserService.getCmsPage(sectionId);
            }
        };

        return new LeftMenuPanel(id, sectionModel, categoryId, pageId);

    }

    @Override
    protected Component newRightPanel(String id) {
        return new EmptyPanel(id);
    }

    @Override
    protected Component newUriPanel(String id) {
        return new EmptyPanel(id);
    }

    class CmsPageModel extends AbstractReadOnlyModel<CmsPage> {

        final String url;

        public CmsPageModel(String url) {
            this.url = url;
        }

        @Override
        public CmsPage getObject() {
            CmsPage myPage = cmsUserService.getCmsPageByUrl(url);
            if (myPage == null) {
                throw new RestartResponseException(PageNotFound.class);
            }
            return myPage;
        }
    }

}

